﻿using Domain.Production_Modules;
using ERP.Domain.Order_Module;
using ERP.ErrorCode;
using ERP.Infratructure.Interface;
using ERP.Server.API.Applocation.Command;
using ERP.Server.API.DTOs;
using MediatR;

namespace ERP.Server.API.Applocation.Handler
{
    public class OrderQueryCommandHandler : IRequestHandler<OrderQueryCommand, APIResult<List<OrderQueryDto>>>
    {
        private readonly IBaseRepository<OrderModel> orderRep;
        private readonly IBaseRepository<ClientModel> clientRepo;
        public OrderQueryCommandHandler(IBaseRepository<OrderModel> orderRep, IBaseRepository<ClientModel> clientRepo)
        {
            this.orderRep = orderRep;
            this.clientRepo = clientRepo;
        }
        public Task<APIResult<List<OrderQueryDto>>> Handle(OrderQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<OrderQueryDto>> result =new APIResult<List<OrderQueryDto>>();
            result.Code=ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();
            var list = from a in orderRep.GetAll()
                       join b in clientRepo.GetAll() on a.ClientId equals b.Id
                       where !a.IsDel
                       select new OrderQueryDto
                       {
                          Id = a.Id,
                            OrderCode = a.OrderCode ,
                            OrderSon = a.OrderSon,
                            OrderState = a.OrderState,
                            OrderGoods = a.OrderGoods,

                            ClientId = b.Id,
                            IsOrder = a.IsOrder,
                            ClientCode = b.ClientCode,
                            ClientName = b.ClientName,
                            ClientTel = b.ClientTel,
                            ClientNickname = b.ClientNickname,
                            ClientAddr = b.ClientAddr,

                            ClientSocial  =b.ClientSocial,
                            ClientDesc = b.ClientDesc,


                       };
                result.Data = list.ToList();    
            return Task.FromResult(result); 
        }
    }
}
